package cp3120.ucm.server;

import cp3120.ucm.*;
import cp3120.dao.DAOFactory;
import cp3120.dao.mysql.MySQLDAOFactory;
import cp3120.net.AirportServer;

/**
 * Precondition: The server is not running
 * Postcondition: The server is running.
 * 1. The system will initilize an appropriate DAOFactory.
 * 2. The system will create a new instance of the server.
 * @author Adam Scarr <scarr.adam@gmail.com>
 */

public class UCStartServer implements UCController {
    private String databaseURL;
    private String userName;
    private String password;

    /**
     * entry point for the server
     * @param args arguements passed from the command line
     */
    public static void main(String[] args) {
        if (args.length == 0) {
            new UCDefaultStartServer().run();
        } else {
            new UCStartServer(args[0], args[1], args[2]).run();
        }
    }

    /**
     * sets the varibles needed to connect tot he database
     * @param databaseURL   The URL of the database to connect too
     * @param userName      the login name of the database
     * @param password      the password for the database
     */
    public UCStartServer(String databaseURL, String userName, String password) {
        this.databaseURL = databaseURL;
        this.userName = userName;
        this.password = password;
    }

    /**
     * run this US
     */
    public void run() {
        // 1. The system will initilize an appropriate DAOFactory.
        DAOFactory.setFactory(new MySQLDAOFactory(databaseURL, userName, password));

        // 2. The system will create a new instance of the server.
        AirportServer.getInstance().start(7778);
    }
}
